package com.topcheer.provider.common.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.topcheer.provider.system.entity.OrgDatasource;
import com.topcheer.provider.system.service.OrgDatasourceService;
import com.topcheer.common.datasource.dynamic.DataSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;

import javax.sql.DataSource;

/**
 * @author xutao
 * @Package com.topcheer.common.datasource.dynamic
 * @Description: ${todo}
 * @date 2020/4/3 14:52
 */
//@Component
public class DruidDataSourceImpl implements DataSourceBuilder {

    @Autowired
    OrgDatasourceService orgDatasourceService;

    @Override
    public DataSource builder(String dataSourceKey, String dataSourceType) {
        OrgDatasource orgDatasource = orgDatasourceService.selectOne(Long.valueOf(dataSourceKey));
        if(orgDatasource == null){
            return null;
        }
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(orgDatasource.getUrl());
        dataSource.setUsername(orgDatasource.getUserName());
        dataSource.setPassword(orgDatasource.getPassword());
        dataSource.setInitialSize(dataSource.getInitialSize());
        dataSource.setMinIdle(dataSource.getMinIdle());
        dataSource.setMaxActive(dataSource.getMaxActive());
        dataSource.setConnectProperties(null);
        return dataSource;
    }
}
